package com.suma.platform.core.util;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author suma
 * @date 2018/2/8 上午10:44
 */
public class ReentrantLockTest {

	private ReentrantLock lock = new ReentrantLock();

	public void test() {
		lock.lock();
		try {
			System.out.println(Thread.currentThread().getName() + " get lock");
			int holdCount = lock.getHoldCount();
			System.out.println(holdCount);
//			lock.lock();
//			try {
//				System.out.println(Thread.currentThread().getName() + " get lock");
//				int holdCount2 = lock.getHoldCount();
//				System.out.println(holdCount2);
//			} finally {
//				// 线程第一次释放锁
//				lock.unlock();
//				System.out.println(Thread.currentThread().getName() + " release lock");
//			}
		} finally {
			// 线程再次释放锁
			lock.unlock();
			System.out.println(Thread.currentThread().getName() + " release lock again");
		}
	}

	public static void main(String[] args) {
		final ReentrantLockTest test = new ReentrantLockTest();
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				test.test();
			}
		}, "A");
		thread.start();
	}
}
